<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-3.1.1.js"></script>
    <script src="js/base.js"></script>
    <script src="js/md5.js"></script>
</head>
<body>
    <h3>手机号一键登录</h3>
    <input id="telephone" placeholder="请输入手机号"/>
    <button onclick="return sendSMS();">获取验证码</button>
    <br>
    <input id="code" placeholder="请输入短信验证码"/>
    <br>
    <button onclick="return signIn();">登陆</button>
<script>
    function signIn(){
        let url="http://localhost:9001/kx/user/onekeysignin";
        let requestJSON={};
        requestJSON.phone=document.getElementById("telephone").value;
        requestJSON.code=document.getElementById("code").value;
        $.rest("post",url,requestJSON,function(data){
            let code = data.code;
            if (code == 200) {
                //登陆成功
                /**
                 * {
                        "code": 200,
                        "msg": "登陆成功",
                        "result": {
                            "user": {
                                "id": 1,
                                "username": "zhangsan",
                                "password": "",
                                "nickname": "张三",
                                "gender": "男",
                                "telephone": "15911135712",
                                "email": "zhangsan@qq.com",
                                "iconLink": "http://xuzhong.vip/icon/zhangsan.jpg"
                            },
                            "token": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiLlvKDkuIkiLCJpYXQiOjE2MjY0MTk4MTAsImV4cCI6MTYyNjQyMTYxMCwidXNlciI6IntcImVtYWlsXCI6XCJ6aGFuZ3NhbkBxcS5jb21cIixcImdlbmRlclwiOlwi55S3XCIsXCJpY29uTGlua1wiOlwiaHR0cDovL3h1emhvbmcudmlwL2ljb24vemhhbmdzYW4uanBnXCIsXCJpZFwiOjEsXCJuaWNrbmFtZVwiOlwi5byg5LiJXCIsXCJwYXNzd29yZFwiOlwiXCIsXCJ0ZWxlcGhvbmVcIjpcIjE1OTExMTM1NzEyXCIsXCJ1c2VybmFtZVwiOlwiemhhbmdzYW5cIn0ifQ.rANwleOoTdXAjL39H_9Cz0alww-LvGo5I7dH0jfzDX4"
                        },
                        "ok": true
                    }
                 */
                let token = data.result.token;
                let id=data.result.user.id;
                let username=data.result.user.username;
                let nickname=data.result.user.nickname;
                let gender=data.result.user.gender;
                let telephone=data.result.user.telephone;
                let email=data.result.user.email;
                let iconLink=data.result.user.iconLink;
                //前端开始保存，各种前端被服务器的支持是一致的。
                //前端H5把数据解析放入localStorage里
                //前端Android、iOS把数据放入SQLite里
                localStorage.setItem("token",token);
                localStorage.setItem("id",id);
                localStorage.setItem("username",username);
                localStorage.setItem("nickname",nickname);
                localStorage.setItem("gender",gender);
                localStorage.setItem("telephone",telephone);
                localStorage.setItem("email",email);
                localStorage.setItem("iconLink",iconLink);
                alert("登陆成功");
            } else if (code == 500) {
                //密码错误
                alert("密码错误");
            } else if (code == 501) {
                //账号已经被锁定
                alert("账号已经被锁定");
            }
        });
        return false;
    }



    function sendSMS(){
        //获取手机号
        let phone=document.getElementById("telephone").value;
        //请求服务器
        let url="http://localhost:9001/kx/user/sms";
        let requestJSON={};
        requestJSON.phone=phone;
        $.rest("post",url,requestJSON,function(data){
            if(data.code==200){
                alert("验证码已经发送");
            }
        });
        return false;
    }
</script>
</body>
</html>